<?PHP
session_start();
session_cache_expire(30);
?>
<!--
	calendar.php
	@author Max Palmer and Allen Tucker
	@version 3/26/08, revised 9/15/2008
-->
<html>
<head>
<title>Calendar display and edit form</title>
<link rel="stylesheet" href="styles.css" type="text/css" />
<style>
.vac{
	background: white;
}
.noVac{
	background: grey;
}

</style>

</head>
<body>
	<div id="container">
		<?PHP include('header.php');?>
		<div id="content">
			<span class="viewweek"> <?php
			include_once('database/dbMonths.php');
			include_once('database/dbPersons.php');
			include_once('database/dbLog.php');
			include_once('database/dbShifts.php');
			include 'calendar.inc';
			
			// gets the month to show, if no month then defaults to current week
			$monthid=$_GET['id'];
			if(!$monthid)
				$monthidm=date("m",time());
				$monthidy=date("y",time());
				$monthid = $monthidm."-01-".$monthidy;
			$month=select_dbMonths($monthid);
			// if invalid week or unpublished week and not a manager
			if (! $month instanceof Month) {
				echo 'This month\'s calendar is not available for viewing. ';
			}
			else {
				show_month($month);
			}
			?> <?PHP include('footer.inc');?>
		
		</div>
	</div>
</body>
</html>

<?php

/**
 * gets a month object, and displays it as a calendar
 */
function show_month($month) {
		// sets up the table, with title, and then day of month
		echo ('<table id="calendar" align="center" ><tr bgcolor=#7991B1 class="weekname"><td colspan="16" ' .
				'align="center" >'.'House Calendar: '.$month->get_name().'</td></tr>
				<tr bgcolor=#7991B1><td>Sunday</td><td>Monday</td><td>Tuesday</td>
				<td>Wednesday</td><td>Thursday</td><td>Friday</td><td>Saturday</td></tr>');

		$dates = $month->get_dates(); //dates is an array of RMH Dates
		
		//for each day, test to see if any of the shifts has a vancany. If it does, add the correct class 
		//so the css will display it correctly
		$shiftNames = ['9-12', '12-15', '15-18', '9-12Fam', '12-15Fam', '15-18Fam', '18-21', '10-13', '13-16', '12-14', '14-17']; //possible shifts
		for ($i=0; $i <($month->get_days()+$month->get_start()); $i++) {	
			
			$date = $dates[$i]; //get the current date
			
			for($i = 0; $i < count($shiftNames) ; $i++ ){ //go through each shift name
				$id = $date->get_id().'-'.$shiftNames[$i]; //make a shift id from the day id and the shift names
				$shift = select_dbShifts($id); //retrieve that shift from the database
				
				if($shift instanceof Shift){ //if it is an instance of Shift
					if($shift->num_vacancies() != 0){ //if there is a vacancy, add the appropriate class
						$bgcolor = "vac";
					}
					else{
						$bgcolor = "noVac";
					}
					
				}
				else{ //else there is no shift and the class is noVac
						$bgcolor = "noVac";
				}
			}

			//display the calendar
			if(($i % 7) == 0 ){
				$weekid = $date->get_id(); 	//if it is the beinning of a week, add a link for that row to view 
								 			// in the week in the calendar
				 echo "<tr>";	 
			}
			if($i < $month->get_start()) echo "<td></td>";
			//add the correct css class, and a link to view the week in the shift view
			else echo "<a href='editShift.php?week='.$weekid.'><td class='.$bgcolor.' align='center' valign='top' height='70px' width='70px'>". ($i - $month->get_start() + 1) . "</td></a>"; 
			if(($i % 7) == 6 ) echo "</tr>";
		}

		echo "</table>";
	}


	?>